package com.derbysoft.dswitch.adapter.ihg.dswitch2.translate;

import com.derbysoft.modules.eip.translator.Translator;
import com.derbysoft.remote.dswitch.core.GenericRQ;
import com.derbysoft.remote.dswitch.hotel.dto.*;
import org.springframework.stereotype.Component;

@Component("hotelReservationRQ2HotelAvailRQTranslator")
public class HotelReservationRQTranslator implements Translator<GenericRQ<HotelReservationRQ>, GenericRQ<HotelAvailRQ>> {

    @Override
    public GenericRQ<HotelAvailRQ> translate(GenericRQ<HotelReservationRQ> request) {
        GenericRQ<HotelAvailRQ> hotelAvailRQ = new GenericRQ<HotelAvailRQ>();
        hotelAvailRQ.setHeader(request.getHeader());
        hotelAvailRQ.setPayload(translateHotelAvailRQ(request.getPayload()));
        return hotelAvailRQ;
    }

    private HotelAvailRQ translateHotelAvailRQ(HotelReservationRQ hotelReservationRQ) {
        HotelAvailRQ hotelAvailRQ = new HotelAvailRQ();
        hotelAvailRQ.setAvailCriteria(translateHotelAvailCriteria(hotelReservationRQ.getHotelReservation()));
        return hotelAvailRQ;
    }

    private HotelAvailCriteriaDTO translateHotelAvailCriteria(HotelReservationDTO hotelReservation) {
        HotelAvailCriteriaDTO hotelAvailCriteria = new HotelAvailCriteriaDTO();
        hotelAvailCriteria.setHotelRef(hotelReservation.getHotelRef());
        hotelAvailCriteria.setLanguageType(hotelReservation.getLanguageType());
        hotelAvailCriteria.setStayDateRange(hotelReservation.getRoomStays().get(0).getStayDateRange());
        hotelAvailCriteria.setRoomStayCandidates(translateRoomStayCandidates(hotelReservation.getRoomStays().get(0)));
        return hotelAvailCriteria;
    }

    private RoomStayCandidateDTO translateRoomStayCandidates(ReservationRoomStayDTO reservationRoomStay) {
        RoomStayCandidateDTO roomStayCandidate = new RoomStayCandidateDTO();
        roomStayCandidate.setGuestCounts(reservationRoomStay.getGuestCounts());
        roomStayCandidate.setNumberOfUnits(reservationRoomStay.getNumberOfUnits());
        return roomStayCandidate;
    }

}
